package com.wd.buycar.view.activity

import android.net.Uri
import androidx.work.WorkInfo
import androidx.work.WorkManager
import com.blankj.utilcode.util.ToastUtils
import com.google.ar.sceneform.Node
import com.google.ar.sceneform.SceneView
import com.google.ar.sceneform.math.Quaternion
import com.google.ar.sceneform.math.Vector3
import com.google.ar.sceneform.rendering.ModelRenderable
import com.wd.buycar.databinding.ActivityCarviewactivityBinding
import com.wd.common.base.BaseActivity
import com.wd.mine.work.DownloadWorker
import java.io.File
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ExecutionException


class CarViewActivity : BaseActivity<ActivityCarviewactivityBinding, Any>() {
    private var transparentSceneView: SceneView? = null
    override fun getViewBinding() = ActivityCarviewactivityBinding.inflate(layoutInflater)

    override fun initData() {
        transparentSceneView = binding.transparentSceneView
        transparentSceneView!!.setTransparent(true)

        val id = intent.getIntExtra("id", 1)
        if(File("/sdcard/Android/data/com.wd.car/cache/car_3d_$id.glb").exists()){
            loadModels(id)
        }else{
            val uuid = DownloadWorker.startDownload(this,"$id","/sdcard/Android/data/com.wd.car/cache","/car_3d_$id.glb")
            WorkManager.getInstance(this).getWorkInfoByIdLiveData(uuid).observe(this){
                when(it.state){
                    WorkInfo.State.SUCCEEDED ->{
                        loadModels(id)
                    }
                    WorkInfo.State.FAILED ->{
                        ToastUtils.showShort("下载失败")
                    }
                    else ->{}
                }
            }
        }
    }
    fun loadModels(id : Int) {
        val dragon = ModelRenderable
            .builder()
            .setSource(
                this, Uri.parse("/sdcard/Android/data/com.wd.car/cache/car_3d_$id.glb")
            )
            .setIsFilamentGltf(true)
            .setAsyncLoadEnabled(true)
            .build()
        CompletableFuture.allOf(dragon)
            .handle<Any?> { ok: Void?, ex: Throwable? ->
                try {
                    val modelNode1 = Node()
                    modelNode1.renderable = dragon.get()
                    modelNode1.localScale = Vector3(0.3f, 0.3f, 0.3f)
                    modelNode1.localRotation = Quaternion.axisAngle(Vector3(0f, 3f, 0f), 35F)
                    modelNode1.localPosition = Vector3(0.3F, -0.2f, -1f)
                    transparentSceneView!!.scene.addChild(modelNode1)
                } catch (ignore: InterruptedException) {
                } catch (ignore: ExecutionException) {
                }
                null
            }
    }
    override fun onResume() {
        super.onResume()
        try {
            transparentSceneView!!.resume()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onPause() {
        super.onPause()
        transparentSceneView!!.pause()
    }
}